package com.xingyang.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xingyang.controller.request.LinkGroupAddRequest;
import com.xingyang.controller.request.LinkUpdateRequest;
import com.xingyang.interceptor.LoginInterceptor;
import com.xingyang.manager.LinkGroupManager;
import com.xingyang.mapper.LinkGroupMapper;
import com.xingyang.model.LinkGroupDO;
import com.xingyang.service.LinkGroupService;
import com.xingyang.vo.LinkGroupVO;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 信仰
 * @since 2023-12-10
 */
@Service
@Slf4j
public class LinkGroupServiceImpl implements  LinkGroupService{


    @Autowired
    private LinkGroupManager linkGroupManager;


    @Override
    public int add(LinkGroupAddRequest addRequest) {

        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();

            LinkGroupDO linkGroupDO=new LinkGroupDO();
             linkGroupDO.setTitle(addRequest.getTitle());
             linkGroupDO.setAccountNo(accountNo);

        int rows= linkGroupManager.add(linkGroupDO );

        return rows;
    }

    @Override
    public int del(Long groupId) {

        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();

       int rows= linkGroupManager.del(groupId,accountNo);
        return rows;
    }

    @Override
    public LinkGroupVO detail(Long groupId) {

        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();

         LinkGroupDO linkGroupDO=   linkGroupManager.detail(groupId,accountNo);

         LinkGroupVO linkGroupVO=new LinkGroupVO();
        BeanUtils.copyProperties(linkGroupDO,linkGroupVO);
        return linkGroupVO;
    }

    @Override
    public List<LinkGroupVO> listAllGroup() {

        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        List<LinkGroupDO> linkGroupDOList=  linkGroupManager.listAllGroup(accountNo);

        List<LinkGroupVO> linkGroupVOList= linkGroupDOList.stream().map(obj->{
            LinkGroupVO linkGroupVO=new LinkGroupVO();

            BeanUtils.copyProperties(obj,linkGroupVO);
            return linkGroupVO;
        }).collect(Collectors.toList());
        return linkGroupVOList;
    }


    @Override
    public int updateByid(LinkUpdateRequest request) {
        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();

        LinkGroupDO linkGroupDO=  new LinkGroupDO();
        linkGroupDO.setTitle(request.getTitle());
        linkGroupDO.setId(request.getId());
         linkGroupDO.setAccountNo(accountNo);
        int rows= linkGroupManager.updateById(linkGroupDO);
        return rows;
    }


}
